import 'package:drive/drive.dart';
import 'package:flutter/material.dart';
import 'package:beautiful_demo/mvc/good/bean/good_bean.dart';
import 'package:beautiful_demo/mvc/good/bean/shop_bean.dart';
import 'view.dart';
import 'package:flutter_mvc/flutter_mvc.dart';

///Description:商品pop
///Author:teihideharu
///date created 2021/04/27
class GoodPopController extends BaseController {
  static int value = 1;
  static int sku = 2;
  static int car = 3;

  int allNum = 0;
  double allPrice = 0.0;

  List<GoodDataSkuThisBean> get thisSku => data.thisSku;

  final GoodDataBean data;
  final int type;
  final String id;

  static dynamic switchPage(int type) {
    if (type == value)
      return GoodValuePopPage();
    else
      return GoodSkuPopPage();
  }

  GoodPopController(this.type, this.id, this.data) : super(switchPage(type));

  GoodDataSkuThisBean selectSpec;

  @override
  void initState() {
    super.initState();
    if (type == sku || type == car) {
      selectSpec = thisSku.first;
    }
  }

  Future<dynamic> push(BuildContext context,
      {RouteBuilder routeBuilder}) async {
    await DDialogUtils.showBottomDialog(context, widget);
  }

  void onSpecClick(GoodDataSkuThisBean item) {
    this.selectSpec = item;
    setState();
  }

  void onSkuNumChange() {
    int num = 0;
    for (var item in selectSpec.child) {
      num += item.num;
    }
    selectSpec.num = num;

    int allNum = 0;
    double allPrice = 0.0;
    for (var spec in thisSku) {
      allNum += spec.num;
      for (var sku in spec.child) {
        allPrice += sku.num * double.parse(sku.price);
      }
    }
    this.allNum = allNum;
    this.allPrice = allPrice;

    setState();
  }

  onPayClick() {}

  onSuccessClick() async {

  }

  onAddCarClick() async {}
}

class GoodPopContactController extends BaseController {
  final ShopDataBean shopData;

  GoodPopContactController(this.shopData) : super(GoodContactPopPage());

  GoodDataSkuThisBean selectSpec;

  Future<dynamic> push(BuildContext context,
      {RouteBuilder routeBuilder}) async {
    DDialogUtils.showBottomDialog(context, widget);
  }
}
